//
//  BuyDetailDigitalViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddOldBuyDetailDigitalViewController.h"
#import "SBJson.h"

@interface JddOldBuyDetailDigitalViewController ()

@end

@implementation JddOldBuyDetailDigitalViewController

@synthesize multipleLabel;
@synthesize MoneyLabel;
@synthesize secrectLabel;
@synthesize WinlotNumberLabel;
@synthesize WinMoneyLabel;
@synthesize issueLabel;
@synthesize lotNameLabel;
@synthesize stateLabel;
@synthesize SchemeNumberLabel;
@synthesize StartTimeLabel;
@synthesize ListTable;
@synthesize realString;
@synthesize playLabel;
@synthesize playHeadLabel;
@synthesize sportType;
@synthesize WinlotNumberLabel2;
@synthesize playtypeLabel;
@synthesize winNumHead;
@synthesize myBuyMoney;
@synthesize myWinMoney;
@synthesize tipLabel;
@synthesize sfcView;
@synthesize sfvListView;
@synthesize weishangchuanView;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddOldBuyDetailDigitalViewController);

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    type = 0;
    [self.navigationItem setTitle:PAGE_BUY_DETAIL];

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DELETE;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (secretType==0) {
            return 0;
    }else {
        if (sportType==0) {
            if ([numberArray2 count]==0) {
                tipLabel.hidden = NO;
            }
            if (tableView == self.sfvListView) {
                return [numberArray3 count];
            }else{
                return [numberArray2 count];
            }
        }else {
            if ([numberArray3 count]==0) {
                tipLabel.hidden = NO;
            }
            return [numberArray3 count];
        }
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (sportType!=0) {
        return 68.0;
    }else {
        if(type == 19){
            return 20.0;
        }
        return 44.0;
    }
        
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tableView == self.sfvListView){
        JddOldSFcBuyDetailItemCell *cell = [JddOldSFcBuyDetailItemCell getInstanceView];
        NSDictionary *ssqNumber = [numberArray3 objectAtIndex:indexPath.row];
        cell.nMu.text = [ssqNumber objectForKey:@"NO"];
        NSString *str = [ssqNumber objectForKey:@"VS"];
        NSArray *teams = [str componentsSeparatedByString:@"vs"];
        if([teams count]>1){
            cell.hTeam.text = [teams objectAtIndex:0];
            cell.vTeam.text = [teams objectAtIndex:1];
        }
        NSString * betnum = [ssqNumber objectForKey:@"Content"];
        if([betnum length] > 10){
            cell.betNum.textColor = [UIColor redColor];
            betnum = [betnum stringByReplacingOccurrencesOfString:@"<font color=#a50000>" withString:@""];
            betnum = [betnum stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
        }else{
            cell.betNum.textColor = [UIColor blackColor];
        }
        cell.betNum.text = betnum;
        NSString *score = [ssqNumber objectForKey:@"Score"];
        if([score length] > 10){
            cell.score.textColor = [UIColor redColor];
            score = [score stringByReplacingOccurrencesOfString:@"<font color=#a50000>" withString:@""];
            score = [score stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
        }else{
            cell.score.textColor = [UIColor blackColor];
        }
        cell.score.text = score;
        NSString *result = [ssqNumber objectForKey:@"Result"];
        if([result length] > 10){
            cell.result.textColor = [UIColor redColor];
            result = [result stringByReplacingOccurrencesOfString:@"<font color=#a50000>" withString:@""];
            result = [result stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
        }else{
            cell.result.textColor = [UIColor blackColor];
        }
        cell.result.text = result;
        return cell;
    }else{
        if (sportType==0) {
            JddOldDigitalBuyItemCell *cell = [JddOldDigitalBuyItemCell getInstanceView];
            NSString *string1 = [[NSString stringWithString:[numberArray2 objectAtIndex:indexPath.row]] stringByReplacingOccurrencesOfString:@"%2B" withString:@"+"];
            NSArray *array2 = [string1 componentsSeparatedByString:@"+ "];
            NSMutableString *string4 = [array2 objectAtIndex:0];
            
            if ([array2 count]>1) {
                cell.number.text = string4;
                cell.number2.text = [array2 objectAtIndex:1];
            }else {
                cell.number.text = string4;
                cell.number2.text = @"";
            }
            
            cell.delegate = self;
            return cell;
            
        }else {
            NSDictionary *ssqNumber = [numberArray3 objectAtIndex:indexPath.row];
            if (type == 9101 || type == 9102 || type == 9104) {
                JddOldSportBuyItemCell *cell = [JddOldSportBuyItemCell getInstanceView];
                cell.vteamNameLabel.text = @"主队";
                cell.hteamNameLabel.text = @"客队";
                cell.rgorrfLabel.text = @"让分:";
                cell.mno.text = [ssqNumber objectForKey:@"MNo"];
                cell.HTeam.text = [ssqNumber objectForKey:@"VTeam"];
                cell.VTeam.text = [ssqNumber objectForKey:@"HTeam"];
                cell.content.text = [ssqNumber objectForKey:@"Content"];
                cell.cg.text = [ssqNumber objectForKey:@"Cg"];
                cell.rq.text = [NSString stringWithFormat:@"%@",[ssqNumber objectForKey:@"Rq"]];
                cell.score.text = [ssqNumber objectForKey:@"Score"];
                if (type == 9104) {
                    cell.rgorrfLabel.text = @"预设分:";
                    cell.rq.text = [ssqNumber objectForKey:@"Ys"];
                }
                cell.delegate =self;
                return cell;
            }
            else{
                JddOldSportBuyItemCell *cell = [JddOldSportBuyItemCell getInstanceView];
                cell.mno.text = [ssqNumber objectForKey:@"MNo"];
                cell.HTeam.text = [ssqNumber objectForKey:@"HTeam"];
                cell.VTeam.text = [ssqNumber objectForKey:@"VTeam"];
                cell.content.text = [ssqNumber objectForKey:@"Content"];
                cell.cg.text = [ssqNumber objectForKey:@"Cg"];
                cell.rq.text = [NSString stringWithFormat:@"%d",[[ssqNumber objectForKey:@"Rq"] intValue]];
                cell.score.text = [ssqNumber objectForKey:@"Score"];
                cell.delegate =self;
                return cell;
            }
        }
        
    }
    return nil;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    weishangchuanView.hidden = YES;
    
    
    tipLabel.hidden = YES;
    sportType = 0;
    type = 0;
    self.playtypeLabel.hidden = YES;
    SBJsonParser *jsonparser = [[SBJsonParser alloc] init];
    NSDictionary *buydetail = [jsonparser objectWithString:realString];
    self.lotNameLabel.text = [buydetail objectForKey:@"LotName"];
    secretType = [[buydetail objectForKey:@"IsCanSee"] intValue];
    tipLabel.text = secretType == 0?[buydetail objectForKey:@"SecState"]:@"未上传";
    secrectLabel.text = [buydetail objectForKey:@"SecState"];
    NSString *string1 = [buydetail objectForKey:@"LotName"];
    NSArray *array1 = [string1 componentsSeparatedByString:@" "];
    self.issueLabel.text = [buydetail objectForKey:@"Issue"];
    self.SchemeNumberLabel.text = [buydetail objectForKey:@"SchemeNumber"];
    self.stateLabel.text = [buydetail objectForKey:@"State"];
    myWinMoney.text = [NSString stringWithFormat:@"%3.1f彩豆",[[buydetail objectForKey:@"MyWinMoney"] floatValue]];
    myBuyMoney.text = [NSString stringWithFormat:@"%3.1f彩豆",[[buydetail objectForKey:@"MyMoney"] floatValue]];
    self.MoneyLabel.text = [NSString stringWithFormat:@"%d彩豆",[[buydetail objectForKey:@"Money"] intValue]];
    self.WinMoneyLabel.text = [NSString stringWithFormat:@"%3.1f彩豆",[[buydetail objectForKey:@"WinMoney"] floatValue]];
    NSString *string3 = [buydetail objectForKey:@"WinLotNumber"];
    NSArray *array2 = [string3 componentsSeparatedByString:@"+ "];
    NSMutableString *string4 = [array2 objectAtIndex:0];
    
    if ([array2 count]>1) {
        self.WinlotNumberLabel.text = string4;
        self.WinlotNumberLabel2.text = [array2 objectAtIndex:1];
    }else {
        self.WinlotNumberLabel.text = string4;
        self.WinlotNumberLabel2.text = @"";
    }
    self.multipleLabel.text = [NSString  stringWithFormat:@"%d",[[buydetail objectForKey:@"Multiple"] intValue]];
    self.StartTimeLabel.text = [buydetail objectForKey:@"StartTime"];
    NSString *tableValue = [buydetail objectForKey:@"LotNumber"];
    NSString *string2 = [array1 objectAtIndex:0];
    NSString *playMe = @"";
    if ([array1 count]>=2) {
        playMe = [array1 objectAtIndex:1];
    }
    NSRange rang = [string1 rangeOfString:@"胜负彩"];
    NSRange rang1 = [string1 rangeOfString:@"任选九"];
    if (![string2 isEqualToString:@"竞彩足球"] && ![string2 isEqualToString:@"竞彩篮球"]&& ![string2 isEqualToString:@"足彩单场"]) {
        sportType = 0;//除了竞彩足球 竞彩篮球 北京单场意外的彩种
        if(rang.location != NSNotFound || rang1.location != NSNotFound){//胜负彩、任选九
            self.sfcView.hidden = NO;
            self.winNumHead.hidden = NO;
            self.playHeadLabel.hidden = YES;
            self.playLabel.hidden = YES;
            type = 19;
            numberArray3 = [jsonparser objectWithString:tableValue];
            [sfvListView reloadData];
            if([numberArray3 count] < 1){
                weishangchuanView.hidden = NO;
            }
        }else{
            type = 0;
            self.sfcView.hidden = YES;
            self.winNumHead.hidden = NO;
            self.playHeadLabel.hidden = YES;
            self.playLabel.hidden = YES;
            numberArray2 = [tableValue componentsSeparatedByString:@"\n"];
            if([numberArray2 count] < 1){
                weishangchuanView.hidden = NO;
            }
            else if([numberArray2 count] == 1){
                NSString *str = [numberArray2 objectAtIndex:0];
                if ([str length]<1) {
                    weishangchuanView.hidden = NO;
                }
            }
        }
    }else {
        type = 0;
        if([string2 isEqualToString:@"竞彩篮球"]){
            if([playMe isEqualToString:@"让分胜负"]){
                type = 9101;
            }else if([playMe isEqualToString:@"胜负"]){
                type = 9102;
            }else if ([playMe isEqualToString:@"大小分差"]){
                type = 9104;
            }
        }
        numberArray = [jsonparser objectWithString:tableValue];
        self.sfcView.hidden = YES;
        self.winNumHead.hidden = YES;
        self.playLabel.text = [numberArray objectForKey:@"PassType"];
        self.playLabel.hidden =  NO;
        self.playHeadLabel.hidden = NO;
        numberArray3 = [numberArray objectForKey:@"SPFMatches"];
        if([numberArray3 count] < 1){
            weishangchuanView.hidden = NO;
        }
        sportType ++;
    }
    
    [ListTable reloadData];
}

- (void)viewDidUnload
{
    [self setSfcView:nil];
    [self setSfvListView:nil];
    [self setWeishangchuanView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
